//
//  MovieDetailVC.m
//  豆瓣
//
//  Created by lanou3g on 15/12/12.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "MovieDetailVC.h"
#import "MovieModel.h"
#import "DataBase_Movie.h"
#import "LandingVC.h"
#import "HTDataBase_DB.h"
#import "Model_DB.h"


@interface MovieDetailVC ()

@property(nonatomic,strong)NSMutableDictionary *dataDic;

@property(nonatomic,strong)UIAlertController *alertController;

@end

@implementation MovieDetailVC




- (void)setData
{
    
    
    
    //接口http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=216609
    


    
    NSString *urlString = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId="];
    
    
    if ((self.model.movieId != nil) && (![self.model.movieId isEqualToString:@""])) {
        
        //6、 字符串拼接
        NSString *movieDetaliURL = [urlString stringByAppendingString:self.model.movieId];
        
        
        
        //    建立链接
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:movieDetaliURL]];
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            if (data != nil) {
                
#warning 警告:给Model 赋值不上。。。。。哭了zo(记住了，可算找到原因了，的回到主线程赋值，疑问是在子线程请求的数据没发赋值....你大爷  FUCK)
                
                
                [self performSelectorOnMainThread:@selector(reloadData:) withObject:data waitUntilDone:YES ]; //回到主线程
                
            }
            
            
        }];
        
        
        [dataTask resume];
        
        
    }
  
    
    
}

- (void)reloadData:(id)sender{
 
//    NSLog(@"%@",sender);
    
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:sender options:0 error:nil];
        self.movieModel = [[MovieModel alloc] init];
           [self.movieModel setValuesForKeysWithDictionary:dic[@"result"]];  //Model赋值
   

    self.navigationItem.title = self.movieModel.title;
    self.movieDetailView.modelmodel = self.movieModel;  //赋值
    
    
//    CGFloat plotHieght = [self myHeihgtBylable:self.movieDetailView.plot_simple_.text];
//    self.movieDetailView.plot_simple_.frame = CGRectMake(0, 0, self.movieDetailView.frame.size.width - 20, self.movieDetailView.frame.size.height - 64 - plotHieght);
//    self.movieDetailView.scrollView.contentSize = CGSizeMake(self.movieDetailView.frame.size.width - 20, plotHieght);
    
  
    
}



- (void)loadView
{
    self.movieDetailView = [[MovieDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.movieDetailView;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.movieDetailView.modelmodel = self.movieModel;
     self.navigationItem.title = self.movieModel.title;
    [self setNavigationItem]; //设置导航栏
    [self setData];
   


    
//    //自适应高度
//        CGFloat plotHieght = [self myHeihgtBylable:self.movieDetailView.plot_simple_.text];
//    
//        self.movieDetailView.scrollView.contentSize = CGSizeMake(self.view.frame.size.width - 20, plotHieght);
//    
//    
//        self.movieDetailView.plot_simple_.frame = CGRectMake(self.movieDetailView.poster_.frame.origin.x, CGRectGetMaxY(self.movieDetailView.QingJie.frame), self.view.frame.size.width - 20, plotHieght - 49);
    
    
}

//设置导航栏
- (void)setNavigationItem
{
    

    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    
    
}
#pragma mark 导航栏按钮事件
- (void)leftAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightAction:(UIBarButtonItem *)sender
{
    
    LandingVC *land = [LandingVC new];
    UINavigationController *navigation_landing = [[UINavigationController alloc] initWithRootViewController:land];
    [navigation_landing.navigationBar setTintColor:nil];
    [navigation_landing.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    land.hidesBottomBarWhenPushed = YES;
    navigation_landing.navigationBar.translucent= NO;
    
    
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *State = [user objectForKey:@"State"];
    
    
    
    if (![State isEqualToString:@"YES"]) {
        
        
        [self showDetailViewController:navigation_landing sender:nil];
        
        
        
    }else{
        
        
//        [[DataBase_Movie shareDataBaseHandle] openDB];
//        NSMutableArray *array = [[DataBase_Movie shareDataBaseHandle] selectmovie];
//        
//        NSMutableArray *array_title = [NSMutableArray array];
//        for (MovieModel *model1 in array) {
//            [array_title addObject:model1.title];
//        }
        [[HTDataBase_DB shareData] createDataBase];
        Model_DB *model = [[Model_DB alloc] init];
        model.title = self.movieModel.title;
        Model_DB *tempModel = [[[HTDataBase_DB shareData] selectModel:model Table:@"Movie"] lastObject];
        
        
        if (tempModel != nil) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已经被收藏过" preferredStyle:UIAlertControllerStyleAlert];
            
            
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil]];
            
            
            [self presentViewController:alertC animated:YES completion:nil];
        }else{


            
            self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            
//            [[DataBase_Movie shareDataBaseHandle] insertMovie:self.movieModel];
            
            [[HTDataBase_DB shareData] insertModel:model Table:@"Movie"];
            
            [self presentViewController:self.alertController animated:YES completion:nil];
            [self.alertController dismissViewControllerAnimated:YES completion:nil];
            //定时器,1秒后 警告视图消失
//            [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
            
        }
        
//        [[DataBase_Movie shareDataBaseHandle] closeDB];
        

        
    }
    
    
}
////定时器,1秒后 警告视图消失
//- (void)performDismiss:(NSTimer *)timer
//{
//    [self.alertController dismissViewControllerAnimated:YES completion:nil];
//}




@end
